package com.atguigu.lock;


class ConsumerDemo {
    private int a = 0;
    public synchronized  void jia() {
        while (a!=0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(Thread.currentThread().getName()+"\t"+(++a));

        this.notifyAll();

    }
    public synchronized void jian(){

        while (a==0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(Thread.currentThread().getName()+"\t"+(--a));

        this.notifyAll();
    }
}

public class Demo{
    public static void main(String[] args) {
        ConsumerDemo consumerDemo = new ConsumerDemo();

        for (int i = 0; i < 10; i++) {
            new Thread(()->{consumerDemo.jia();},"A").start();
        }

        for (int i = 0; i < 10; i++) {
            new Thread(()->{consumerDemo.jian();},"B").start();
        }
        for (int i = 0; i < 10; i++) {
            new Thread(()->{consumerDemo.jia();},"C").start();
        }
        for (int i = 0; i < 10; i++) {
            new Thread(()->{consumerDemo.jian();},"D").start();
        }
    }

}

